﻿
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using demo.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demo
{
    public class TransformDemo
    {


        private ExternalCommandData _commandData;

        protected readonly Document _doc;

        public TransformDemo(ExternalCommandData commandData)
        {
            _commandData = commandData;
            _doc = _commandData.Application.ActiveUIDocument.Document;
        }
        public void Test()
        {
            //https://zhuanlan.zhihu.com/p/99697122


            // 旋转();
            //平移();
            //投影();
            //Test2();

            //偏移2();


            var arc1 = Arc.Create(XYZ.Zero, 20, 0, 180 * Math.PI / 180, XYZ.BasisX, XYZ.BasisY);
            var arc2 = Arc.Create(XYZ.Zero, 20, 180 * Math.PI / 180, 2*180 * Math.PI / 180, XYZ.BasisX, XYZ.BasisY);

            D(arc1);
            D(arc2);

        }

        /// <summary>
        /// 旋转、平移、缩放和镜像转换
        /// </summary>
        public void Test1()
        {

            XYZ p1 = new XYZ(2.0, 5.0, -7.5);
            XYZ p2;
            XYZ p3 = new XYZ(1.0, 2.0, 3.0);

            XYZ v1 = new XYZ(1.0, 4.0, 5.0);
            XYZ v2 = new XYZ(0.0, 1.0, -1.0);
            XYZ v3;
            //点和向量 加减法
            p2 = p1 + v1; 
            p1 = p1 + v1; 
            p3 = p3 - v1; 
            v3 = v1 + v2; 
            v1 = v1 + v2; 
            v3 = v1 - v2;

            v2 = v1.Negate(); // v2= -v1
            v1 = v1.Negate(); // v1=-v1

            v1 = v1.Multiply(2.0); // v1 长度翻倍
            v3 = v1.Divide(2.0); // v3=1/2*v1  
            v1 = v1.Normalize(); // v1 单位向量
            double len = v2.GetLength(); // 距离长度  就是这个点到原点的距离
            len = p1.DistanceTo(p2); // p1 到 p2 的距离
            var angle = v1.AngleTo(v2);  // 2个三维向量的夹角

            len = v1.DotProduct(v2); //此向量与指定向量的点乘（一个向量和它在另一个向量上的投影的长度的乘积）
            v3 = v1.CrossProduct(v2); //两个向量之间的叉乘（结果为两个向量构成平面的法向量，遵守右手定则）

            //Transform trans1, trans2, trans3;
            //XYZ ptOrigin = XYZ.Zero;
            //XYZ ptXAxis = XYZ.BasisX;
            //XYZ ptYAxis = XYZ.BasisY;

            //trans1 = Transform.CreateRotation(XYZ.BasisX, 90); //旋转90
            //p3 = trans1.OfPoint(p2);

        }

        public void D(Curve curve)
        {
            Transaction t = new Transaction(_doc, "info12");
            t.Start();
            curve.CreateModelCurve(_doc);
            t.Commit();
        }

        public void 旋转()
        {
            var p01 = new XYZ(0, 0, 0);
            var p02 = new XYZ(0, 0, 2);
            var l1 = Line.CreateBound(p01, p02);
            D(l1);
            //x轴旋转90
            var trans1 = Transform.CreateRotation(XYZ.BasisX, 90 * (Math.PI / 180)); //旋转90
            var p02_1 = trans1.OfPoint(p02);
            D(Line.CreateBound(p01,p02_1));

            //y轴旋转90
            var trans2 = Transform.CreateRotation(XYZ.BasisY, 90 * (Math.PI / 180)); //旋转90
            var p02_2 = trans2.OfPoint(p02);
            D(Line.CreateBound(p01, p02_2));


        }
        public void 平移()
        {
            var p01 = new XYZ(0, 0, 0);
            var p02 = new XYZ(0, 0, 2);
            var l1 = Line.CreateBound(p01, p02);
            D(l1);
            //
            var trans1 = Transform.CreateTranslation(XYZ.BasisX); 
            var p02_1 = trans1.OfPoint(p02);
            D(Line.CreateBound(p01, p02_1));
            var d= l1.CreateTransformed(Transform.CreateTranslation(new XYZ(0, 0, 1)));
            D(d);
        }

        public void 投影()
        {
            //var p01 = new XYZ(0, 0, 0);
            //var p02 = new XYZ(0, 0, 2);
            //var l1 = Line.CreateBound(p01, p02);
            //D(l1);
            //var plan1= Plane.CreateByNormalAndOrigin(XYZ.BasisX,XYZ.BasisY);
            //var trans = Transform.CreateReflection(plan1);//
            //var p02_1 = trans.OfPoint(p02);
            //D(Line.CreateBound(p01, p02_1));

            var p01 = new XYZ(0, 0, 0);
            var p02 = new XYZ(0, 0, 2);
            var l1 = Line.CreateBound(p01, p02);
            D(l1);
            // 点到线上的投影
             var ds= l1.Project(new XYZ(0,0,1));
            



        }

        public void Test2()
        {
           

            // Bound 是线段
            Line line1 = Line.CreateBound(new XYZ(2, 1, 0), new XYZ(2, 5, 0));
            D(line1);
            XYZ p = new XYZ(4, 6, 0);
            D(Line.CreateBound(new XYZ(0, 0, 0), p));
            var d = p.DistanceTo(new XYZ(0,0,0));//两点之间的距离


            //https://blog.csdn.net/lushibi/article/details/41544627
            double dist = line1.Distance(p);  // 点到线的距离   // Bound 是线段   这里的距离是  Line.MakeUnbound() 等于说是延长了直线

            D(Line.CreateBound(p, new  XYZ(2, 1, 0)));

        }


        public void 偏移2()
        {
            Line line1 = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(10, 0, 0));

            D(line1);


            line1.CreateTransformed(Transform.CreateTranslation(new XYZ(0,0,5)));


            D(line1);

        }




    }
}
